#include "game_reward.h"

Game_Reward::Game_Reward(Sence_Config* config){
	this->timer_tick = 0;
	this->p_config = config;
	this->computeDxDyDw();
	Game_Part::LoadPicture(this->pictures,this->p_config->Reward_Num,this->p_config->Reward_Folder);
	this->initialReward();
}

Game_Reward::~Game_Reward(){
	Game_Part::Destroy(this->pictures);
	Game_Part::Destroy(this->rewards);
}

void Game_Reward::computeDxDyDw(){
	double totald = Game_Part::SumPow2(this->p_config->Reward_Tick_Per_Pic);
	double pixels = 0;
	UI::UI_Point& left_p1 = this->p_config->Left_Reward_Min_Point;
	UI::UI_Point& left_p2 = this->p_config->Left_Reward_Max_Point;
	pixels = left_p2.XPos() - left_p1.XPos();
	this->left_dx = pixels / totald;
	pixels = left_p2.YPos() - left_p1.YPos();
	this->left_dy = pixels / totald;
	pixels = this->p_config->Left_Reward_Max_Width - this->p_config->Left_Reward_Min_Width;
	this->left_dw = pixels / totald;

	UI::UI_Point& mid_p1 = this->p_config->Mid_Reward_Min_Point;
	UI::UI_Point& mid_p2 = this->p_config->Mid_Reward_Max_Point;
	pixels = mid_p2.XPos() - mid_p1.XPos();
	this->mid_dx = pixels / totald;
	pixels = mid_p2.YPos() - mid_p1.YPos();
	this->mid_dy = pixels / totald;
	pixels = this->p_config->Mid_Reward_Max_Width - this->p_config->Mid_Reward_Min_Width;
	this->mid_dw = pixels / totald;

	UI::UI_Point& right_p1 = this->p_config->Right_Reward_Min_Point;
	UI::UI_Point& right_p2 = this->p_config->Right_Reward_Max_Point;
	pixels = right_p2.XPos() - right_p1.XPos();
	this->right_dx = pixels / totald;
	pixels = right_p2.YPos() - right_p1.YPos();
	this->right_dy = pixels / totald;
	pixels = this->p_config->Right_Reward_Max_Width - this->p_config->Right_Reward_Min_Width;
	this->right_dw = pixels / totald;
}

void Game_Reward::initialReward(){
	this->addRightReward();
}

void Game_Reward::addLeftReward(){
	int x,y,width;
	SDL_Surface* p = NULL;
	int	a = MyRand::Instance()->Next() % this->p_config->Reward_Num;
	UI::UI_Point& p1 = this->p_config->Left_Reward_Min_Point;
	y = p1.YPos();
	x = p1.XPos();
	width = this->p_config->Left_Reward_Min_Width;
	p = Game_Image::ScaleSurface(this->pictures[a],width);
	Pic* pic = new Pic(x,y,this->left_dx,this->left_dy,this->left_dw,p,this->pictures[a]);
	this->rewards.push_front(pic);
}
void Game_Reward::addRightReward(){
	int x,y,width;
	SDL_Surface* p = NULL;
	int	a = MyRand::Instance()->Next() % this->p_config->Reward_Num;
	UI::UI_Point& p1 = this->p_config->Right_Reward_Min_Point;
	y = p1.YPos();
	x = p1.XPos();
	width = this->p_config->Right_Reward_Min_Width;
	p = Game_Image::ScaleSurface(this->pictures[a],width);
	Pic* pic = new Pic(x,y,this->right_dx,this->right_dy,this->right_dw,p,this->pictures[a]);
	this->rewards.push_front(pic);
}
void Game_Reward::addMidReward(){
	int x,y,width;
	SDL_Surface* p = NULL;
	int	a = MyRand::Instance()->Next() % this->p_config->Reward_Num;
	UI::UI_Point& p1 = this->p_config->Mid_Reward_Min_Point;
	y = p1.YPos();
	x = p1.XPos();
	width = this->p_config->Mid_Reward_Min_Width;
	p = Game_Image::ScaleSurface(this->pictures[a],width);
	Pic* pic = new Pic(x,y,this->mid_dx,this->mid_dy,this->mid_dw,p,this->pictures[a]);
	this->rewards.push_front(pic);
}

void Game_Reward::moveNext(){
	list<Pic*>::iterator it = this->rewards.begin();
	while(it != this->rewards.end()){
		Pic* p = (*it);
		if(!p->Move()){
			it = this->rewards.erase(it);
			SDL_FreeSurface(p->GetSurface());
			delete p;
			int a = MyRand::Instance()->Next() % 3;
			if(a == 0){
				this->addLeftReward();
			}
			else if(a == 1){
				this->addMidReward();
			}
			else{
				this->addRightReward();
			}
		}
		else{
			++it;
		}
	}
}

bool Game_Reward::TimerTick(){
	bool flag = false;
	++ this->timer_tick;
	if(!(this->timer_tick % this->p_config->Reward_Tick_Num)){
		this->moveNext();
		flag = true;
	}
	return flag;
}

void Game_Reward::UpdateToScreen(){
	for(list<Pic*>::iterator it = this->rewards.begin(); it != this->rewards.end(); ++ it){
		Game_Part::Add(*it);
	}
}
